// import OpenAI from "openai"
// const openai = new OpenAI({
//   baseURL: 'https://api.deepseek.com',
//   apiKey: 'sk-ab8ae73a167c43e9ac124f7ff15292ca'
// });

import { getConfig } from '@/api/user'
import CustomForm from '../Form/index.vue'
import ExportBox from '@/views/campus/account/ExportBox'
import Records from '@/views/campus/account/Records'
export default {
  name: 'Login',
  components: { CustomForm},
  data: function() {
    return {
      formVisible: false,
      loginForm: {
        account: '',
        password: '',
        code: '',
        origin: 'password'
      },
      loginRules: {
        account: [
          {
            required: true,
            trigger: 'blur',
            message: this.$t('login.accountTip')
          }
        ],
        password: [
          {
            required: true,
            trigger: 'blur',
            message: this.$t('login.passwordTip')
          }
        ]
      },
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  computed: {
    loginLoading() {
      return this.$store.state.user.loginLoading
    }
  },
  watch: {
    loginLoading(val) {
      if (!val) this.loading = false
    },
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
    const _this = this
    document.onkeydown = function(e) {
      const { keyCode } = e
      if (keyCode === 13) {
        _this.handleLogin()
      }
    }
  },
  mounted() {
    // if (this.loginForm.account == '') {
    //   this.$refs.account.focus()
    // } else if (this.loginForm.password == '') {
    //   this.$refs.password.focus()
    // }
    this.$store.commit('user/SET_LOGIN_LOADING', false)
  },
  destroyed() {
    document.onkeydown = function(e) {
      const { keyCode } = e
      if (keyCode === 13) {
      }
    }
  },
  methods: {
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && key >= 'A' && key <= 'Z'
    },
    handleLogin() {
      if (this.loading) return
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.commit('user/SET_LOGIN_LOADING', true)
          this.$store
            .dispatch('user/login', this.loginForm)
            .then((res) => {
              if (res.checkFirstLogin) {
                this.formVisible = true
                this.$nextTick(() => {
                  this.$refs.CustomForm.init()
                })
              } else {
                this.$router.push({
                  path: this.redirect || '/home',
                  query: this.otherQuery
                })
              }
            })
            .catch(() => {
              this.$store.commit('user/SET_LOGIN_LOADING', false)
            })
        } else {
          return false
        }
      })
    },


    getConfig(val) {

    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
